using System;
using System.Collections.Generic;
using System.Text;
using MacomberMap.Common.Components;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace MacomberMap.Common.User_Interfaces.Network_Map.Components
{
    /// <summary>
    /// This class holds the graphical representation of a line
    /// </summary>
    public class MM_NetworkMap_Line : MM_NetworkMap_Element
    {
        #region Variable declarations
        /// <summary>The line upon which this graphical represenation is based</summary>
        public MM_Line BaseLine;

        /// <summary>Our graphics path for the line</summary>
        private GraphicsPath LinePath;

        /// <summary>A low-resolution rectangle corresponding to the extrema of the line, for rapid hit-testing</summary>
        private Rectangle LowResRectangle;

        /// <summary>Whether the line is visible </summary>
        public bool Visible = false;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new graphical representation of a line
        /// </summary>
        /// <param name="BaseLine">The line upon which the graphics are based</param>
        /// <param name="Coordinates">The system coordinates</param>
        public MM_NetworkMap_Line(MM_Line BaseLine, MM_NetworkMap_Display Coordinates)
        {
            this.BaseLine = BaseLine;
            this.LinePath = new GraphicsPath();
            RecomputeLine(Coordinates);
        }
        #endregion

        #region Coordinates handling
        /// <summary>
        /// Recompute a line's positioning
        /// </summary>
        /// <param name="Coordinates"></param>
        public void RecomputeLine(MM_NetworkMap_Display Coordinates)
        {
            Point Stn1 = MM_NetworkMap_Display.LatLngToXY(BaseLine.Substation1.LatLong, Coordinates.ZoomLevel);
            Point Stn2 = MM_NetworkMap_Display.LatLngToXY(BaseLine.Substation2.LatLong, Coordinates.ZoomLevel);

            this.LinePath.ClearMarkers();
            this.LinePath.AddLine(Stn1, Stn2);

            LowResRectangle = new Rectangle(Math.Min(Stn1.X, Stn2.X), Math.Min(Stn1.Y, Stn2.Y), Math.Max(Stn1.X, Stn2.X) - Math.Min(Stn1.X, Stn2.X), Math.Max(Stn1.Y, Stn2.Y) - Math.Min(Stn1.Y, Stn2.Y));
        }

        /// <summary>
        /// Test to see whether a line is hit, based on its pixel coordinates
        /// </summary>
        /// <param name="CoordinateXY"></param>
        /// <returns></returns>
        public override bool HitTest(Point CoordinateXY)
        {
            if (!LowResRectangle.Contains(CoordinateXY))
                return false;
            else
                return LinePath.IsOutlineVisible(CoordinateXY, new Pen(Color.Brown, 3f));
        }
        #endregion
    }
}
